package com.agent.common.interceptor;

import com.agent.common.entity.UserContext;
import com.agent.common.handler.JwtAuthHandler;
import com.agent.common.utils.JwtUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 认证拦截器
 */
@Component
public class AuthInterceptor implements HandlerInterceptor {

    private final JwtAuthHandler authHandler;

    public AuthInterceptor(JwtUtils jwtUtils) {
        this.authHandler = new JwtAuthHandler(jwtUtils);
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        UserContext userContext = authHandler.authenticate(request);
        request.setAttribute("userContext", userContext);
        return true;
    }
}